package com.deyuanyun.pic.settings.service.base.impl;

import com.deyuanyun.pic.common.util.MapUtil;
import com.deyuanyun.pic.settings.domain.base.Attachment;
import com.deyuanyun.pic.settings.domain.base.AttachmentExample;
import com.deyuanyun.pic.settings.mapper.base.AttachmentMapper;
import com.deyuanyun.pic.settings.service.base.AttachmentService;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class AttachmentServiceImpl implements  AttachmentService{

	private @Resource AttachmentMapper attachmentMapper;

	@Override
    public List<Attachment> queryAttachment(String businessId) {
	    AttachmentExample example = new AttachmentExample();
        example.createCriteria().andBusinessIdEqualTo(businessId).andDeletedEqualTo((short)0);
        example.setOrderByClause("create_date desc");
        List<Attachment> list = attachmentMapper.selectByExample(example);
        return list;
    }

    @Override
    public Attachment queryAttachmentTop1(String businessId) {
        List<Attachment> list = queryAttachment(businessId);
        if(list == null || list.isEmpty()){
            return null;
        }
        return list.get(0);
    }

    @Override
    public List<Attachment> queryAttachment(String businessId, String fileType) {
        AttachmentExample example = new AttachmentExample();
        example.createCriteria().andBusinessIdEqualTo(businessId).andFileTypeEqualTo(fileType).andDeletedEqualTo((short)0);
        example.setOrderByClause("create_date desc");
        List<Attachment> list = attachmentMapper.selectByExample(example);
        return list;
    }

    @Override
    public Attachment queryAttachmentTop1(String businessId, String fileType) {
        List<Attachment> list = queryAttachment(businessId,fileType);
        if(list == null || list.isEmpty()){
            return null;
        }
        return list.get(0);
    }

	@Override
	public int insertSelective(Attachment record) {
		return attachmentMapper.insertSelective(record);
	}

	@Override
	public int updateBusinessIdById(String businessId, String id) {

		return attachmentMapper.updateBusinessIdById(businessId,id);
	}

	@Override
	public int insert(Map<String,String> param) {
		Attachment record;
		try {
			record = (Attachment) MapUtil.mapToObject(param, Attachment.class);
			return attachmentMapper.insert(record);
		} catch (InstantiationException e) {
			e.printStackTrace();
	} catch (IllegalAccessException e) {
		e.printStackTrace();
	} catch (NoSuchMethodException e) {
		e.printStackTrace();
	} catch (SecurityException e) {
		e.printStackTrace();
	} catch (IllegalArgumentException e) {
		e.printStackTrace();
	} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		return  -1;
	}

	@Override
	public int update(Map<String,String> param) {
		Attachment record;
		try {
			record = (Attachment) MapUtil.mapToObject(param, Attachment.class);
			return attachmentMapper.updateByPrimaryKey(record);
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		return  -1;
		
	}

	@Override
	public int deleteByPrimaryKey(String id) {
		return attachmentMapper.deleteByPrimaryKey(id);
	}

	@Override
	public int updateByBusinessId(String businessId) {
		return 0;
	}

	public List<Attachment> queryAttachmentOrderByCustom(String businessId) {
		List<Attachment> list = new ArrayList<Attachment>();
		//修复前
		String customType = "repairBeforeImage";
		list =queryAttachmentByCustom(businessId,customType);
		//修复中
		customType = "repairInImage";
		list.addAll(queryAttachmentByCustom(businessId,customType));
		//修复后
		customType = "repairAfterImage";
		list.addAll(queryAttachmentByCustom(businessId,customType));
		//焊接作业指导书
		customType = "weldingImage";
		list.addAll(queryAttachmentByCustom(businessId,customType));
		//无损检测报告
		customType = "examiningImage";
		list.addAll(queryAttachmentByCustom(businessId,customType));
		return list;
	}

	@Override
	public List<Attachment> queryAttachmentByCustom(String businessId, String customType) {
		AttachmentExample example = new AttachmentExample();
		example.createCriteria().andBusinessIdEqualTo(businessId).andCustomTypeEqualTo(customType).andDeletedEqualTo((short)0);
		example.setOrderByClause("create_date desc");
		List<Attachment> list = attachmentMapper.selectByExample(example);
		return list;
	}

	@Override
	public Attachment queryAttachmentByCustomOne(String businessId, String customType) {
		List<Attachment> list = queryAttachmentByCustom(businessId, customType);
		if(list == null || list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

	@Override
	public int updateInsert(Map<String, String> param) {
		Attachment record;
		try {
			record = (Attachment) MapUtil.mapToObject(param, Attachment.class);
			
			if(param.get("create") != null)
			{
				//String id=UUID.randomUUID().toString().replaceAll("-", "");
				//param.put("id", id);
				record.setCreateUser(param.get("user"));
				record.setCreateDate(new Date());
				record.setCreateRemark(param.get("ps"));
				record.setId(param.get("id"));
				record.setRealName(param.get("realName"));
				record.setSource(param.get("source"));
				record.setBusinessId(param.get("bid")); 
				return attachmentMapper.insertSelective(record);
			}else{
				record.setUpdateUser(param.get("user"));
				record.setUpdateDate(new Date());
				record.setUpdateRemark(param.get("ps"));
				record.setRealName(param.get("realName"));
				return attachmentMapper.updateByPrimaryKeySelective(record);
			}
			
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		return 0;
	}
}
